<template>
  <div class="add">
    <BreadCrumb :item="['商品管理', '添加商品']"></BreadCrumb>
    <!--  -->
    <el-card>
      <!-- 进度条 -->
      <el-steps
        :space="200"
        :active="step - 1"
        finish-status="success"
        align-center
      >
        <el-step title="基本信息"></el-step>
        <el-step title="商品参数"></el-step>
        <el-step title="商品属性"></el-step>
        <el-step title="商品图片"></el-step>
        <el-step title="商品内容"></el-step>
        <el-step title="完成"></el-step>
      </el-steps>
      <!-- 左标签页 -->
      <el-tabs
        tab-position="left"
        v-model="step"
        :before-leave="isCatelist"
        @tab-click="getAttr"
      >
        <el-tab-pane name="1" label="基本信息">
          <el-form :model="addForm" :rules="rules">
            <el-form-item label="商品名称" prop="all">
              <el-input v-model="addForm.goods_name"></el-input>
            </el-form-item>
            <el-form-item label="商品价格" prop="all">
              <el-input v-model="addForm.goods_price"></el-input>
            </el-form-item>
            <el-form-item label="商品数量" prop="all">
              <el-input v-model="addForm.goods_number"></el-input>
            </el-form-item>
            <el-form-item label="商品重量" prop="all">
              <el-input v-model="addForm.goods_weight"></el-input>
            </el-form-item>
            <el-form-item label="商品分类" prop="all">
              <el-cascader
                v-model="goods_cat"
                :options="goodsCategories"
                :props="treeProps"
                @change="handleChange"
              >
              </el-cascader>
            </el-form-item>
          </el-form>
        </el-tab-pane>
        <el-tab-pane name="2" label="商品参数"></el-tab-pane>
        <el-tab-pane name="3" label="商品属性">
          <el-form>
            <el-form-item
              v-for="item in goodsAttrList"
              :key="item.attr_id"
              :label="item.attr_name"
              prop="all"
            >
              <el-input :value="item.attr_vals"></el-input>
            </el-form-item>
          </el-form>
        </el-tab-pane>
        <el-tab-pane name="4" label="商品图片">
          <el-upload
            class="upload-demo"
            action="https://www.liulongbin.top:8888/api/private/v1"
            :on-preview="handlePreview"
            :on-remove="handleRemove"
            :file-list="fileList"
            list-type="picture"
          >
            <el-button size="small" type="primary">点击上传</el-button>
          </el-upload>
        </el-tab-pane>
        <el-tab-pane name="5" label="商品内容">
          <quillEditor></quillEditor>
          <el-button class="subBtn" type="primary" @click="submitGoods"
            >添加商品</el-button
          >
        </el-tab-pane>
      </el-tabs>
    </el-card>
  </div>
</template>

<script>
import { getGoodsAttribute, uploadGoods } from '@/api/goods'
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'

import { quillEditor } from 'vue-quill-editor'
import { Message } from 'element-ui'
import { mapState } from 'vuex'
export default {
  name: '',
  components: {
    quillEditor,
  },
  data() {
    return {
      step: '1',
      goodsAttrList: [],
      // goodsCategories: [],
      goods_cat: [],
      fileList: [],
      addForm: {
        goods_name: '',
        goods_price: '',
        goods_number: '',
        goods_weight: '',
      },
      treeProps: {
        label: 'cat_name',
        value: 'cat_id',
        children: 'children',
        expandTrigger: 'hover',
      },
      rules: {
        all: [{ required: true, message: '输入不能为空', trigger: 'blur' }],
      },
    }
  },
  computed: {
    ...mapState('goods', ['goodsCategories']),
  },
  methods: {
    // async initCategories() {
    //   const res = await getGoodsCategories()
    //   // console.log(res)
    //   this.goodsCategories = res.data
    //   // console.log(this.goodsCategories)
    // },
    // 次联选择器
    handleChange(value) {
      // console.log(value)
      this.goods_cat = value
    },
    // 是否可离开
    isCatelist() {
      if (!this.goods_cat.length) {
        Message.error('请选择商品分类')
        return false
      }
    },
    // 预览图片
    handlePreview(file) {
      console.log(file)
    },
    handleRemove(file, fileList) {
      console.log(file, fileList)
    },
    // 添加商品
    async submitGoods() {
      await uploadGoods({
        ...this.addForm,
        goods_cat: this.goods_cat.toString(),
      })
      this.$router.push('/goods')
    },
    // 获取商品属性
    async getAttr(tab) {
      if (tab.name === '3') {
        const res = await getGoodsAttribute(
          this.goods_cat[this.goods_cat.length - 1],
          {
            sel: 'only',
          }
        )
        // console.log(res)
        this.goodsAttrList = res.data
      }
    },
  },
  created() {
    // this.initCategories()
    this.$store.dispatch('goods/initCategories')
  },
}
</script>

<style lang="less" scoped>
.add {
  .el-steps {
    margin-bottom: 15px;
  }
  /deep/ .el-form-item__label {
    float: none;
  }
  /deep/ .ql-container {
    height: 300px;
  }
  .subBtn {
    margin-top: 15px;
  }
}
</style>
